﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using VB.Entries.DataModel;

namespace VB.Entries.SQLManager
{
    public sealed class TransactionContext : IDisposable, IConnectionContext
    {
        private SqlConnection _connection;
        private SqlTransaction _transaction;

        internal TransactionContext(SqlConnection connection)
        {
            _connection = connection;
            _connection.Open();
            _transaction = _connection.BeginTransaction(IsolationLevel.Serializable);

        }

        public VBBlogModelDataContext CreateContext()
        {
            VBBlogModelDataContext result = new VBBlogModelDataContext(_connection);
            result.Transaction = _transaction;
            return result;
        }

        public void Dispose()
        {
            _transaction.Dispose();
            _connection.Dispose();
        }

        public void Commit()
        {
            _transaction.Commit();
        }
    }
}
